package com.wf.demo.sys.service;

import com.wf.demo.sys.dao.RoleDAO;
import com.wf.demo.sys.pojo.Role;
import com.wf.demo.sys.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: wf
 * @Date: 2019/12/19 20:25
 * @Description:
 */
@Service
public class RoleService {
    @Autowired
    RoleDAO roleDAO;
    public List<Role> list(){
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        return roleDAO.findAll(sort);
    }
    public Role get( long id){
       return  roleDAO.findOne(id);
    }
    public void delete(long id){
        roleDAO.delete(id);
    }
    public void update(Role bean){
        roleDAO.save(bean);
    }
    public void add( Role bean){
        roleDAO.save(bean);
    }
    public Boolean isExist(String roleName){
       Role role=roleDAO.findByRoleName(roleName);
        return null!=role;
    }
    public boolean isExist(Role bean) {
        //找到id
      long  id =bean.getId();
        //先得到对应id的对象在数据库的名字
        String oldName=roleDAO.getOne(id).getRoleName();
        String name=bean.getRoleName();
        //如果名字没改变
        if (oldName.equals(name)){
            return false;
        }
        Role  role= roleDAO.findByRoleName(name);
        return null!=role;
    }
}
